import axios from 'axios';

function getGoods(){
    return axios.get('/api/goods');
}
export default {
    namespace: 'goods',
    state: {
        courses:{}, //课程
        tags:[] //分类
    },
    effects: {  //异步操作(redux-saga机制)
        *getList(action,{call,put}){
            const res = yield call(getGoods); //异步请求
            yield put({type:'initGoods',payload:res.data.data});//action提交到reducer
        }
    },
    reducers: { //更新状态
        addGood(state,action){
            return [...state,{title:action.payload.title}]
        },
        initGoods(state,{payload}){
            //初始化课程和分类
            const {tags,data:courses} = payload;
            return {...state,tags,courses};
        }
    }
}